#if 0   // UNused

#include <stdio.h>
#include <stdlib.h>	// for qsort
#include <math.h>
#include "Otdr.h"

int temp_in_func[DATA_LEN];

/*
 **************************************************************************************************
 *  潞炉脢媒  脙没拢潞 kth_smallest
 *  潞炉脢媒脙猫脢枚拢潞 脩掳脮脪脢媒脳茅脌茂碌脷k脨隆脰碌
 *  脠毛驴脷虏脦脢媒拢潞 
 *  路碌禄脴虏脦脢媒拢潞 
 *  脠脮脝脷掳忙卤戮拢潞 2011-05-12  14:39:04  v1.0
 **************************************************************************************************
*/
#if 1

#define ELEM_SWAP_F(a,b) {float temp = (a); (a) = (b); (b) = temp;}

float kth_smallest_f(float buf[], int n, int k)
{
    register int i,j,low,high;
    float x;

        low = 0, high = n-1;
        while(low < high)
        {
            x = buf[k];
            i = low, j = high;
            do
            {
                while(buf[i] < x)     i++;
                while(x < buf[j])	    j--;
                if(i <= j)
                {
                    ELEM_SWAP_F(buf[i], buf[j]);
                    i++, j--;
                }
            }while(i <= j);
            if(j < k) low  = i;
            if(k < i) high = j;
        }
        return buf[k];
}
#endif

/*
 **************************************************************************************
 *  潞炉脢媒脙没鲁脝拢潞 qMedianFilter
 *  潞炉脢媒脙猫脢枚拢潞 露脭脢盲脠毛脨脜潞脜陆酶脨脨驴矛脣脵脰脨脰碌脗脣虏篓拢卢虏禄脰搂鲁脰脭颅碌脴脰路录脝脣茫
 *  脠毛驴脷虏脦脢媒拢潞 input     : 脢盲脠毛脨脜潞脜
 *             output    : 脢盲鲁枚脨脜潞脜
 *             FilterLen : 脗脣虏篓脝梅鲁陇露脠拢卢驴陋麓掳麓贸脨隆脦陋 2*FilterLen + 1
 *             DataLen   : 脢媒戮脻鲁陇露脠
 *  路碌禄脴虏脦脢媒拢潞 
 *  脠脮脝脷掳忙卤戮拢潞 2011-04-12  10:25:48  v1.0
 **************************************************************************************
*/
int qMedianFilter(const int input[], int output[], int FilterLen, int DataLen)
{
    extern int32_t kth_smallest(int32_t buf[], int n, int k);
    int i, j, k, n = 2*FilterLen+1;
    int *buf, *left, *right, temp, med, left_num, right_num;
    int left_count = 0, center_count = 0, right_count = 0;
    
/********************************** 路脰脜盲脗脣虏篓麓掳驴脷buf **********************************************/
    if(FilterLen < 2)   return 0;
    buf = temp_in_func;

/************************ 陆芦虏禄脗脣虏篓碌脛脢盲脠毛脢媒戮脻赂麓脰脝碌陆脢盲鲁枚 *******************************************/
    output[0] = input[0];
    for(i = 1; i < FilterLen; i++){
        for(k = 0, j = 0; j <= 2*i; j++, k++)   buf[k] = input[j];
        output[i] = kth_smallest(buf, k, k>>1);    // get median
    }
    
/********************************** 驴陋脢录脰脨脰碌脗脣虏篓 *************************************************/
    // 脭脷陆芦脢媒戮脻赂麓脰脝碌陆脗脣虏篓脝梅禄潞麓忙脟掳脧脠掳麓戮脡脰脨脰碌陆酶脨脨脪禄麓脦路脰脳茅脜脜脨貌
    med = input[FilterLen];
    for(i = FilterLen; i < DataLen-FilterLen; i++)
    {
/***************************** 陆芦脗脣虏篓脝梅驴陋麓掳脛脷碌脛脢媒戮脻赂麓脰脝碌陆buf *************************************/
        left = buf;         // 脰赂脧貌buf碌脛脳卯脳贸卤脽
        right = buf+n-1;    // 脰赂脧貌buf碌脛脳卯脫脪卤脽
        for(j = i-FilterLen; j <= i+FilterLen; j++)
        {
            temp = input[j];
            if(temp < med)          *left++  = temp;     // 脨隆碌脛路脜脳贸卤脽
            else if(temp > med)     *right-- = temp;     // 麓贸碌脛路脜脫脪卤脽
        }
        left_num  = left - buf;
        right_num = buf+n-1 - right;
/******************************** 露脭buf陆酶脨脨脰脨脰碌驴矛脣脵脜脜脨貌 ******************************************/
        // 脠莽鹿没脳贸卤脽脢媒戮脻赂枚脢媒鹿媒掳毛拢卢脭貌脜脜脨貌脳贸卤脽脢媒戮脻
        if(left_num > FilterLen-1)   // 脦脪脨猫脪陋碌脛脢脟脜脜脨貌潞贸脢媒脳茅脌茂脧脗卤锚脦陋 FilterLen-1 碌脛脰碌拢卢录麓脰脨脰碌脳贸卤脽碌脛脛脟赂枚
        {
            left_count++;
            output[i] = kth_smallest(buf, left_num, FilterLen-1);
        }
        // 脠莽鹿没脫脪卤脽脢媒戮脻赂枚脢媒鹿媒掳毛拢卢脭貌脜脜脨貌脫脪卤脽脢媒戮脻
        else if(right_num > FilterLen+1)
        {
            right_count++;
            output[i] = kth_smallest(right+1, right_num, right_num+FilterLen-n-1);
        }
        else    // 路帽脭貌脰脨脰碌虏禄卤盲拢卢脠脭脢脟脡脧脪禄麓脦脢盲鲁枚碌脛脰脨脰碌
        {
            center_count++;
            output[i] = med;
        }
/******************************** 赂眉脨脗脰脨脰碌 *******************************************************/
        med = output[i];
    }

    for(i = DataLen-FilterLen; i < DataLen-1; i++){
        for(k = 0, j = 2*i-DataLen+1; j < DataLen; j++, k++)   buf[k] = input[j];
        output[i] = kth_smallest(buf, k, k>>1);  // get median
    }
    output[DataLen-1] = input[DataLen-1];
/******************************** 脢脥路脜脛脷麓忙 *******************************************************/
	return 1;
}

int qfMedianFilter(const float input[], float output[], int FilterLen, int DataLen)
{
    int i, j, k, n = 2*FilterLen+1;
    int left_num, right_num;
	float *buf, *left, *right, temp, med;
    int left_count = 0, center_count = 0, right_count = 0;
    
/********************************** 路脰脜盲脗脣虏篓麓掳驴脷buf **********************************************/
    if(FilterLen < 2)   return 0;
    buf = (float *)temp_in_func;

/************************ 陆芦虏禄脗脣虏篓碌脛脢盲脠毛脢媒戮脻赂麓脰脝碌陆脢盲鲁枚 *******************************************/
    output[0] = input[0];
    for(i = 1; i < FilterLen; i++){
        for(k = 0, j = 0; j <= 2*i; j++, k++)   buf[k] = input[j];
        output[i] = kth_smallest_f(buf, k, k>>1);    // get median
    }
    
/********************************** 驴陋脢录脰脨脰碌脗脣虏篓 *************************************************/
    // 脭脷陆芦脢媒戮脻赂麓脰脝碌陆脗脣虏篓脝梅禄潞麓忙脟掳脧脠掳麓戮脡脰脨脰碌陆酶脨脨脪禄麓脦路脰脳茅脜脜脨貌
    med = input[FilterLen];
    for(i = FilterLen; i < DataLen-FilterLen; i++)
    {
/***************************** 陆芦脗脣虏篓脝梅驴陋麓掳脛脷碌脛脢媒戮脻赂麓脰脝碌陆buf *************************************/
        left = buf;         // 脰赂脧貌buf碌脛脳卯脳贸卤脽
        right = buf+n-1;    // 脰赂脧貌buf碌脛脳卯脫脪卤脽
        for(j = i-FilterLen; j <= i+FilterLen; j++)
        {
            temp = input[j];
            if(temp < med)          *left++  = temp;     // 脨隆碌脛路脜脳贸卤脽
            else if(temp > med)     *right-- = temp;     // 麓贸碌脛路脜脫脪卤脽
        }
        left_num  = left - buf;
        right_num = buf+n-1 - right;
/******************************** 露脭buf陆酶脨脨脰脨脰碌驴矛脣脵脜脜脨貌 ******************************************/
        // 脠莽鹿没脳贸卤脽脢媒戮脻赂枚脢媒鹿媒掳毛拢卢脭貌脜脜脨貌脳贸卤脽脢媒戮脻
        if(left_num > FilterLen-1)   // 脦脪脨猫脪陋碌脛脢脟脜脜脨貌潞贸脢媒脳茅脌茂脧脗卤锚脦陋 FilterLen-1 碌脛脰碌拢卢录麓脰脨脰碌脳贸卤脽碌脛脛脟赂枚
        {
            left_count++;
            output[i] = kth_smallest_f(buf, left_num, FilterLen-1);
        }
        // 脠莽鹿没脫脪卤脽脢媒戮脻赂枚脢媒鹿媒掳毛拢卢脭貌脜脜脨貌脫脪卤脽脢媒戮脻
        else if(right_num > FilterLen+1)
        {
            right_count++;
            output[i] = kth_smallest_f(right+1, right_num, right_num+FilterLen-n-1);
        }
        else    // 路帽脭貌脰脨脰碌虏禄卤盲拢卢脠脭脢脟脡脧脪禄麓脦脢盲鲁枚碌脛脰脨脰碌
        {
            center_count++;
            output[i] = med;
        }
/******************************** 赂眉脨脗脰脨脰碌 *******************************************************/
        med = output[i];
    }

    for(i = DataLen-FilterLen; i < DataLen-1; i++){
        for(k = 0, j = 2*i-DataLen+1; j < DataLen; j++, k++)   buf[k] = input[j];
        output[i] = kth_smallest_f(buf, k, k>>1);  // get median
    }
    output[DataLen-1] = input[DataLen-1];
/******************************** 脢脥路脜脛脷麓忙 *******************************************************/
	return 1;
}

/*
 ********************************************************************
 *    End    of    File
 ********************************************************************
*/
#endif

